package com.kgd.news.utils;

import com.jfinal.upload.UploadFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/9
 * @Time: 下午11:16
 */
public class FileHelper {
    public static  String basePath = "";
//    情趣学院的配置
    public static String getFileHostUrl(HttpServletRequest request, UploadFile file){
        String extension = file.getFileName().substring(file.getFileName().lastIndexOf("."));
        String name = System.currentTimeMillis() + "" + extension;
        String targetPath =  Config.getFileBasePath() + name;
        //String targetPath = "/Users/huangsanm/Downloads/" + name;
        File newFile = new File(targetPath);
        boolean flag = file.getFile().renameTo(newFile);
        System.out.println("FileHelper.getFileHostUrl::" + flag);
        if(flag){
            String path = getFileVituralPath(request, name);
            System.out.println("FileHelper.flag::" + path);
            return path;
        }else{
            return "";
        }
    }

    public static String getFileVituralPath(HttpServletRequest request, String path){
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + "/admin/upload/" + path;
    }



}
